package com.anjiplus.template.gaea.business.modules.quartzReport.task;


import com.anji.plus.gaea.utils.ApplicationContextUtils;
import com.anjiplus.template.gaea.business.modules.dataset.service.DataSetService;
import com.anjiplus.template.gaea.business.modules.datasetparam.controller.dto.DataSetParamDto;
import com.anjiplus.template.gaea.business.modules.datasettransform.service.DataSetTransformService;
import com.anjiplus.template.gaea.business.modules.datasource.service.DataSourceService;
import com.anjiplus.template.gaea.business.modules.https.service.HttpService;
import com.anjiplus.template.gaea.business.modules.quartz.task.QuartzTaskJob;
import com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.entity.TBScheduledGaeaReport;
import com.anjiplus.template.gaea.business.modules.tbscheduledgaeareport.service.TBScheduledGaeaReportService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
@PersistJobDataAfterExecution
@DisallowConcurrentExecution

public class QuartzReportTask03 implements QuartzTaskJob {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        String name = context.getJobDetail().getJobBuilder().build().getKey().getName();
//        ApplicationContext applicaiton = ApplicationContextUtils.getBean();
        DataSetTransformService dataSetTransformService = ApplicationContextUtils.getBean(DataSetTransformService.class);
        DataSourceService dataSourceService = ApplicationContextUtils.getBean(DataSourceService.class);
        DataSetService dataSetService = ApplicationContextUtils.getBean(DataSetService.class);
        TBScheduledGaeaReportService tBScheduledGaeaReportService = ApplicationContextUtils.getBean(TBScheduledGaeaReportService.class);
        QueryWrapper<TBScheduledGaeaReport> TBScheduledGaeaReportWrapper = new QueryWrapper<>();
        TBScheduledGaeaReportWrapper.eq("\"scheduled_code\"", name);
        TBScheduledGaeaReportWrapper.eq("\"delete_state\"", 0);
        //启用之后才执行定时
        TBScheduledGaeaReportWrapper.eq("\"enable_status\"", 1);
        List<TBScheduledGaeaReport> tbScheduledGaeaReports = tBScheduledGaeaReportService.list(TBScheduledGaeaReportWrapper);
        if (!CollectionUtils.isEmpty(tbScheduledGaeaReports)) {
            for (TBScheduledGaeaReport tbScheduledGaeaReport : tbScheduledGaeaReports) {
                System.out.println("定时开始执行了");
                String gaeaReportCode = tbScheduledGaeaReport.getGaeaReportCode();
                HttpService httpService = ApplicationContextUtils.getBean(HttpService.class);
                try {
                    httpService.sendRequest(gaeaReportCode);
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }


            }
        }
    }

    /**
     * dataSetParamDtoList转map
     *
     * @param dataSetParamDtoList
     * @return
     */
    public Map<String, Object> setContextData(List<DataSetParamDto> dataSetParamDtoList) {
        Map<String, Object> map = new HashMap<>();
        if (null != dataSetParamDtoList && dataSetParamDtoList.size() > 0) {
            dataSetParamDtoList.forEach(dataSetParamDto -> map.put(dataSetParamDto.getParamName(), dataSetParamDto.getSampleItem()));
        }
        return map;
    }

}

